package com.gymmsystem.GYMMSystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gymmsystem.GYMMSystem.entity.Announcement;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface AnnouncementMapper extends BaseMapper<Announcement> {

    @Select("SELECT * FROM announcements WHERE status = 1 AND scheduled_publish_time < NOW() ORDER BY scheduled_publish_time ASC")
    List<Announcement> findAllAnnouncements();

    @Select("SELECT * FROM announcements WHERE status != 2 ORDER BY created_at DESC")
    List<Announcement> selectAllAnnouncements();

    @Update("UPDATE announcements SET title = #{title}, content = #{content}, type = #{type}, status = #{status}, scheduled_publish_time = #{scheduledPublishTime}, updated_at = NOW() WHERE id = #{id}")
    int updateAnnouncementById(Announcement announcement);

    @Update("UPDATE announcements SET status = 2, updated_at = NOW() WHERE id = #{id}")
    int deleteAnnouncementById(@Param("id") Long id);

    /**
     * 查询需要发布的预约公告
     * @param currentTime 当前时间
     * @return 需要发布的公告列表
     */
    @Select("SELECT * FROM announcements " +
            "WHERE status = 0 " + // 状态为草稿
            "AND scheduled_publish_time IS NOT NULL " + // 有预约发布时间
            "AND scheduled_publish_time <= #{currentTime}") // 预约时间已到
    List<Announcement> selectScheduledAnnouncements(@Param("currentTime") LocalDateTime currentTime);
}